// 创建 axios 实例
const request = axios.create({
    // API 请求的默认前缀
    baseURL: '/cms/api/',
    timeout: 6000 // 请求超时时间
})

// 异常拦截处理器
const errorHandler = (error) => {
    if (error.response) {
        const data = error.response.data
        layer.msg(data.msg);
    }
    return Promise.reject(error)
}

// request interceptor
request.interceptors.request.use(config => {
    config.headers['X-Requested-With'] = 'xmlHttpRequest'
    let token = Cookies.get('authorization')

    // 登录 Authorization 表单令牌
    if (token) {
        config.headers['Authorization'] = token
    }
    return config
}, errorHandler)

// response interceptor
request.interceptors.response.use((response) => {
    return response.data
}, errorHandler)
